﻿using System;
using Eassets.Layered.Domain;
using System.Data;
using System.Data.SqlClient;
using System.Collections.ObjectModel;

namespace Eassets.Data
{
    public class CompanyDetailsData
    {
        public void Insert(CompanyDetails companyDetails)
        {
            SqlCommand command = new SqlCommand("SP_InsertCompanyDetails"); // Name of the SP
            command.Parameters.AddWithValue("@CompanyCode", companyDetails.CompanyCode);
            command.Parameters.AddWithValue("@LocationCode", companyDetails.LocationCode);
            command.Parameters.AddWithValue("@LocationName", companyDetails.LocationName);
            try
            {
                CommonData commonData = new CommonData(); commonData.ExecuteNonQuery(ConstructCommand(command, companyDetails));
            }
            catch (Exception e)
            {

            }

        }

        public SqlCommand ConstructCommand(SqlCommand command, CompanyDetails companyDetails)
        {
            command.CommandType = CommandType.StoredProcedure;
            return command;
        }

        public void Delete(int locationId)
        {
            SqlCommand command = new SqlCommand("SP_DeleteCompanyDetails");

            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.AddWithValue("@LocationId", locationId);

            CommonData commonData = new CommonData(); commonData.ExecuteNonQuery(command);
        }

        public void Update(CompanyDetails companyDetails)
        {
            SqlCommand command = new SqlCommand("SP_UpdateCompanyDetails");
            command.Parameters.AddWithValue("@LocationId", companyDetails.LocationId);
            command.Parameters.AddWithValue("@CompanyCode", companyDetails.CompanyCode);
            command.Parameters.AddWithValue("@LocationCode", companyDetails.LocationCode);
            command.Parameters.AddWithValue("@LocationName", companyDetails.LocationName);

            CommonData commonData = new CommonData(); commonData.ExecuteNonQuery(ConstructCommand(command, companyDetails));
        }

        public Collection<CompanyDetails> GetAllCompanyDetails()
        {
            SqlCommand command = new SqlCommand("GetAllCompanyDetails");
            command.CommandType = CommandType.StoredProcedure;


            CommonData commonData = new CommonData(); SqlDataReader dataReader = commonData.ExecuteReader(command);

            Collection<CompanyDetails> CompanyDetailss = new Collection<CompanyDetails>();
            CompanyDetails companyDetails;

            while (dataReader.Read())
            {
                companyDetails = new CompanyDetails();
                companyDetails.LocationId = Convert.ToInt16(dataReader["LocationId"]);
                if (String.IsNullOrEmpty(Convert.ToString(dataReader["LocationId"])))
                {
                    companyDetails.LocationId = 0;
                }
                else
                {
                    companyDetails.LocationId = Convert.ToInt16(dataReader["LocationId"]);
                }
                companyDetails.LocationCode = Convert.ToString(dataReader["LocationCode"]);
                companyDetails.LocationName = Convert.ToString(dataReader["LocationName"]);

                CompanyDetailss.Add(companyDetails);
            }
            dataReader.Close();
            return CompanyDetailss;
        }
    }
}
